package com.uma.hadoop.practice;

import org.apache.hadoop.fs.FsUrlStreamHandlerFactory;
import org.apache.hadoop.io.IOUtils;

import java.io.InputStream;
import java.net.URL;

/**
 * @ClassName URLCat
 * @Description 以标准输出方式显示Hadoop文件系统中的文件
 * @Author zhaolu
 * @Date 2021/2/13 6:27 PM
 * @Version 1.0
 **/
public class URLCat {

    private static final String HDFSURI = "hdfs://localhost:9000";


    static {
        URL.setURLStreamHandlerFactory(new FsUrlStreamHandlerFactory());
    }

    public static void main(String[] args) throws Exception
    {
        String fileName = "/user/zhaolu/input/core-site.xml";
        StringBuilder sb = new StringBuilder();
        sb.append(HDFSURI);
        sb.append(fileName);
        System.out.println(sb.toString());
        InputStream in = null;
        try{
            in = new URL(sb.toString()).openStream();
            IOUtils.copyBytes(in,System.out,4096,false);
        }finally {
            IOUtils.closeStream(in);
        }
    }
}
